<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (!function_exists('load_ci')) {

	function load_ci() {
		return get_instance();
	}

}
if (!function_exists('month')) {
	function month() {
		$ci = load_ci();

		for($x=1;$x<=12;$x++)
		{
			$month[$x]=date('F', mktime(0,0,0,$x,1));
		}
		return $month;
	}
}
if (!function_exists('day')) {
	function day() {
		$ci = load_ci();

		for($x=1;$x<=31;$x++)
		{
			$day[$x]=$x;
		}
		return $day;
	}
}
if (!function_exists('year')) {
	function year() {
		$ci = load_ci();
		
		$yr_s = date("Y");
		$yr_e = date("Y")*1-90;
		for($x=$yr_s;$x>$yr_e;$x--)
		{
			$year[$x]=$x;
		}
		return $year;
	}
}
if (!function_exists('cut_string')) {
	function cut_string($string, $max_length) {
		$ci = load_ci();
		if (strlen($string) > $max_length){
			$string = substr($string, 0, $max_length);
			$pos = strrpos($string, " ");
			if($pos === false) {
					return substr($string, 0, $max_length)."...";
			}
				return substr($string, 0, $pos)."...";
		}else{
			return $string;
		}
	}
}
if (!function_exists('cut_name')) {
	function cut_name($string, $max_length) {
		$ci = load_ci();
		if (strlen($string) > $max_length){
			$string = substr($string, 0, $max_length);
			$pos = strrpos($string, " ");
			if($pos === false) {
					return substr($string, 0, $max_length);
			}
				return substr($string, 0, $pos);
		}else{
			return $string;
		}
	}
}
if (!function_exists('extract_words')) {
	function extract_words($string) {
		$ci = load_ci();

		$stopWords = array('i','a','about','in','an','and','are','your','you','have','into','than','as','at','be','by','com','de','en','for','from','how','is','it','la','of','on','or','their','that','the','this','to','was','what','when','where','who','will','with','und','the','www','which','doesnt','without','them');
		
		$string = preg_replace('/\s\s+/i', '', $string); // replace whitespace
		$string = trim($string); // trim the string
		$string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too…
		$string = strtolower($string); // make it lowercase
		
		preg_match_all('/\b.*?\b/i', $string, $matchWords);
		$matchWords = $matchWords[0];
		
		foreach ( $matchWords as $key=>$item ) {
		  if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
			  unset($matchWords[$key]);
		  }
		}   
		$wordCountArr = array();
		if ( is_array($matchWords) ) {
		  foreach ( $matchWords as $key => $val ) {
			  $val = strtolower($val);
			  if ( isset($wordCountArr[$val]) ) {
				  $wordCountArr[$val]++;
			  } else {
				  $wordCountArr[$val] = 1;
			  }
		  }
		}
		arsort($wordCountArr);
		$wordCountArr = array_slice($wordCountArr, 0, 250);
		return $wordCountArr;
	}
}
if (!function_exists('clean_string')) {
	function clean_string($string) {
		$ci = load_ci();
		
		$string = str_replace('', '-', $string); // Replaces all spaces with hyphens.
		return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
	}
}
if (!function_exists('separate_words')) {
	function separate_words($string) {
		$ci = load_ci();
		
		$words = clean_string(extract_words($string));
		$wordsArray = array_keys($words);
		$x = 0;
		foreach($wordsArray as $str)
		{
			$countWords = count($wordsArray)-1;
			if($countWords!=$x){
				$wordSeparator = ',';
			} else {
				$wordSeparator = '';
			}
			echo preg_replace('/(\w{2,})(?=.*?\\1)\W*/', '', $str) .$wordSeparator;
			$x++;
		}
	}
}
if (!function_exists('encrypt_decrypt')) {
	function encrypt_decrypt($action, $string) {
	    $output = false;

	    $encrypt_method = "AES-256-CBC";
	    $secret_key = '%^Kr4m04$';
	    $secret_iv = '$40m4rK^%';

	    // hash
	    $key = hash('sha256', $secret_key);
	    $num = '0';
	    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
	    $iv = substr(hash('sha256', $secret_iv), $num, 16);

	    if( $action == 'encrypt' ) {
	        $output = openssl_encrypt($string, $encrypt_method, $key, $num, $iv);
	        $output = base64_encode($output);
	    }
	    else if( $action == 'decrypt' ){
	        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, $num, $iv);
	    }

	    return $output;
	}
}
if (!function_exists('country_to_abbr')) {
	function country_to_abbr($country) {
		$CI =& get_instance();
	   
		$CI->config->load( 'countries' );
	   
		$country_list = $CI->config->item( 'country_list' );
		$abbr = '';
		foreach( $country_list as $key => $value )
		{
			if($value==strtoupper($country))
			{
				$abbr .= ucwords( strtoupper( $key ) );
			}
		}
	   
		return $abbr;
	}
}
if (!function_exists('convertHashTag')) {
	function convertHashTag($tags) {
		$CI =& get_instance();
		$regex = '/#+([a-zA-Z0-9_]+)/';
		$tag = str_replace('#','','$0');
		$hash = preg_replace($regex, '<a href="'.base_url().'blogs/tags/$1" title="'.$tag.'">$0</a>', $tags);
		return $hash;
	}
}
if (!function_exists('convertHashKey')) {
	function convertHashKey($tags) {
		$CI =& get_instance();
		$regex = '/#+([a-zA-Z0-9_]+)/';
		$hash = preg_replace($regex, '$0', $tags);
		return $hash;
	}
}
if (!function_exists('timeAgo')) {
	function timeAgo($time){
		if ($time !== intval($time)) { $time = strtotime($time); }
		$d = time() - $time;
		if ($time < strtotime(date('Y-m-d 00:00:00')) - 60*60*24*3) {
		$format = 'F j, Y at g:ia';
		if (date('Y') !== date('Y', $time)) {
		$format .= ", Y";
		}
		return date($format, $time);
		}
		if ($d >= 60*60*24) {
		$day = 'Yesterday';
		if (date('l', time() - 60*60*24) !== date('l', $time)) { $day = date('l', $time); }
		return $day . " at " . date('g:ia', $time);
		}
		if ($d >= 60*60*2) { return intval($d / (60*60)) . " hours ago"; }
		if ($d >= 60*60) { return "about an hour ago"; }
		if ($d >= 60*2) { return intval($d / 60) . " minutes ago"; }
		if ($d >= 60) { return "about a minute ago"; }
		if ($d >= 2) { return intval($d) . " seconds ago"; }
		return "a few seconds ago";
	}
}
if (!function_exists('get_age')) {
	function get_age($birthday){
		list($year,$month,$day) = explode("-",$birthday);
		$year_diff  = date("Y") - $year;
		$month_diff = date("m") - $month;
		$day_diff   = date("d") - $day;
		if ($day_diff < 0 && $month_diff==0){$year_diff--;}
		if ($day_diff < 0 && $month_diff < 0){$year_diff--;}
		return $year_diff;
	}
}
